package com.yingda.electronicscaleadvices.utils;

import android.os.Handler;
import android.view.View;

public class DoubleClickUtil {

    private volatile static DoubleClickUtil doubleClick = null;
    private final Handler handler = new Handler();

    public static DoubleClickUtil getInstance() {
        if (null == doubleClick) {
            synchronized (DoubleClickUtil.class) {
                if (null == doubleClick) {
                    doubleClick = new DoubleClickUtil();
                }
            }
        }
        return doubleClick;
    }

    public interface OnDoubleClickListener {
        /**
         * 单击双击事件接口
         *
         * @param v
         */
        void onDoubleClick(View v);
    }


    /**
     * 通过Handler延时发送判断是否双击
     *
     * @param view
     * @param listener
     */
    public void doubleClickListener(View view, final OnDoubleClickListener listener) {
        if (listener == null) {
            return;
        }
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (handler.hasMessages(0)) {
                    //执行双击
                    listener.onDoubleClick(v);
                } else {
                    handler.sendEmptyMessageDelayed(0, 350);
                }
            }
        });
    }

}
